package org.ccvi.policy.service.userinfo;

import java.util.List;

import org.ccvi.policy.dao.userinfo.PolicyResourceDao;
import org.ccvi.policy.dao.userinfo.PolicyRoleDao;
import org.ccvi.policy.dao.userinfo.PolicyUserDao;
import org.ccvi.policy.entity.userinfo.Resource;
import org.ccvi.policy.entity.userinfo.Role;
import org.ccvi.policy.entity.userinfo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

public class UserInfoManager {
	
	@Autowired
	private PolicyUserDao userDao;
	@Autowired
	private PolicyRoleDao roleDao;
	@Autowired
	private PolicyResourceDao resourceDao;

	/** user operation */
	@Transactional(readOnly = true)
	public User getUser(Long id) {
		return userDao.get(id);
	}

	public List<User> getAllUser() {
		List<User> userList = userDao.getAll();
		return userList;
	}

	public void saveUser(User user) {
		userDao.save(user);
	}

	public void deleteUser(Long id) {
		userDao.delete(id);
	}

	@Transactional(readOnly = true)
	public boolean isNameUnique(String username, String oldName) {
		return userDao.isPropertyUnique("username", username, oldName);
	}

	@Transactional(readOnly = true)
	public Page<User> queryUser(Page<User> page, List<PropertyFilter> filters) {
		return userDao.findPage(page, filters);
	}

	public List<User> findUserByName(List<PropertyFilter> filters) {
		return userDao.find(filters);
	}

	/** role operation */
	@Transactional(readOnly = true)
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

	public List<Role> getAllRole() {
		List<Role> roleList = roleDao.getAll();
		return roleList;
	}

	public void saveRole(Role role) {
		roleDao.save(role);
	}

	public void deleteRole(Long id) {
		roleDao.delete(id);
	}

	/** resource operation */
	@Transactional(readOnly = true)
	public Resource getResource(Long id) {
		return resourceDao.get(id);
	}

	public List<Resource> getAllResource() {
		List<Resource> resourceList = resourceDao.getAll();
		return resourceList;
	}

	public void saveResource(Resource resource) {
		resourceDao.save(resource);
	}

	public void deleteResource(Long id) {
		resourceDao.delete(id);
	}

}
